/**
 * 分页
 */
$(document).ready(function() {
	
	var contextPath = $(".commonUrl").text(); //绝对路径
	var info_id=$(".info_id").text();//资讯id
	var selectedPageNum = $(".pageNum").val(); //选中页码
	var datatimeUrl = $(".datatimeUrl").text();
	var timer=$(".timer").val();
	//---------------------------------分页----------------------------------------------

	var pages = $(".pages").text(); //页数
	/*alert(pages)
	var eachpages = 0;
	var num = pages % 5;
	if(num!=0){
		eachpages = (pages - num) / 5 + 1;
	}else{
		eachpages = pages / 5;
	}*/
	//var commentpages = "";
	var pageIndex = $(".pageIndex").text(); //当前页码
	var hasNext = $(".hasNext").text(); //是否有下一页(boolen)
	
	if (pages == 1) {
		$(".prev,.next").css("cursor", "default");
		$(".prev,.next").css("color", "rgb(166, 166, 166)");
		$(".prev,.next").attr("href", "javascript:void(0)");
	} else {
		if (hasNext == "false") {
			$(".next").css("cursor", "default");
			$(".next").css("color", "rgb(166, 166, 166)");
			$(".next").attr("href", "javascript:void(0)");
		}
		if (pageIndex == 1) {
			$(".prev").css("cursor", "default");
			$(".prev").css("color", "rgb(166, 166, 166)");
			$(".prev").attr("href", "javascript:void(0)");
		}
	}
	for (var i = 1; i < pages;) {
		var result = "";
		i = i + 1;
		result += "<option" + " value=" + "'" + i + "'" + " >" + "第" + i + "页" + "</option>";
		$(".pageNum").append(result);
	}

	//----------------------------------异步刷新---------------------------------------------

	$(".pageNum").change(function() {
		var changePageNum = $(".pageNum").val(); //选中页码
		var selfUrl = $(".selfUrl").text(); //请求action
		var ajax_url = contextPath + selfUrl;
		$.ajax({
			url: contextPath+"/comment/get_comment.do",
			data: {
				object_id:info_id,
				object_type:4,
				pageindex:changePageNum,
				pagesize:5
			},
			success: function(data) {
				$(".tech-news-grids").empty();
				$.each(data.list, function(n, commentVal) {
					var result = "";
					result += "<div id='afterCommentDiv' class='afterCommentDiv'>";
					result += '<img alt="30x30" class="img-circle img-opcaity" style="width:50px;height:50px" src="';
					result += commentVal.user.photo_link;
					result +='">';
					result += "<p>" + commentVal.user.username + "</p>";
					result += "<p>" + commentVal.content + "</p>";
					result += '<p style="color:#999">发布于：<fmt:formatDate value="';
					result += commentVal.datetime;
					result += '" pattern="yyyy-MM-dd"/></p>';
					result += "</div>";
					$(".tech-news-grids").append(result);
				})
				$(".pageNum option[value=" + pageIndex + "]").attr("selected", true);
			}
		})
		selectedPageNum=changePageNum;
	})
	
	//------------------------------------上一页下一页------------------------------------
	
	$(".prev").click(function() {
					selectedPageNum--;
					var selfUrl = $(".selfUrl").text(); //请求action
					var ajax_url = contextPath + selfUrl;
					
					$.ajax({
						url: contextPath+"/comment/get_comment.do",
						data: {
							object_id:info_id,
							object_type:4,
							pageindex:selectedPageNum,
							pagesize:5
						},
						success: function(data) {
							$(".tech-news-grids").empty();
							$.each(data.list, function(n, commentVal) {
								var result = "";
								result += "<div id='afterCommentDiv' class='afterCommentDiv'>";
								result += '<img alt="30x30" class="img-circle img-opcaity" style="width:50px;height:50px" src="';
								result += commentVal.user.photo_link;
								result +='">';
								result += "<p>" + commentVal.user.username + "</p>";
								result += "<p>" + commentVal.content + "</p>";
								result += '<p style="color:#999">发布于：<fmt:formatDate value="';
								result += commentVal.datetime;
								result += '" pattern="yyyy-MM-dd"/></p>';
								result += "</div>";
								$(".tech-news-grids").append(result);
							})
							$(".pageNum option[value=" + selectedPageNum + "]").attr("selected", true);
						}
					})
					if (0 == selectedPageNum) {
						$(".prev").removeAttr('href');
						$(".prev").addClass('invalid');
					} else {
						$(".prev").attr("href", "javaScript:;");
						$(".prev").removeClass('invalid');
					}
					$(".next").attr("href", "javaScript:;");
					$(".next").removeClass('invalid');
				})
				$(".next").click(function() {
					selectedPageNum++;
					
					var selfUrl = $(".selfUrl").text(); //请求action
					var ajax_url = contextPath + selfUrl;
					$.ajax({
						url: contextPath+"/comment/get_comment.do",
						data: {
							object_id:info_id,
							object_type:4,
							pageindex:selectedPageNum,
							pagesize:5
						},
						success: function(data) {
							$(".tech-news-grids").empty();
							$.each(data.list, function(n, commentVal) {
								
								var result = "";
								result += "<div id='afterCommentDiv' class='afterCommentDiv'>";
								result += '<img alt="30x30" class="img-circle img-opcaity" style="width:50px;height:50px" src="';
								result +=commentVal.user.photo_link;
								result +='"/>';
								result += "<p>" + commentVal.user.username + "</p>";
								result += "<p>" + commentVal.content + "</p>";
								result += '<p style="color:#999">发布于：<';
								result += datatimeUrl;
								result += 'value="';
								result += commentVal.datetime;
								result += '" pattern="yyyy-MM-dd"/></p>';
								result += "</div>";
								$(".tech-news-grids").append(result);
							})
							$(".pageNum option[value=" + selectedPageNum + "]").attr("selected", true);
						}
					})
					
						if (selectedPageNum == (pages-1)) {
							$(".next").removeAttr('href');
							$(".next").addClass('invalid');
						} else {
							$(".next").attr("href", "javaScript:;");
							$(".next").removeClass('invalid');
						}
					$(".prev").attr("href", "javaScript:;");
					$(".prev").removeClass('invalid');
				})
})